
//队列
function Queue() {
    //队列的节点
    let Node = function (key) {
        this.key = key;
        this.next = null;
        this.pre = null;
    }

    //队列的头
    let head = null;
    //队列的尾
    let tail = null;

    //入队
    this.push = function (key) {
        let newNode = new Node(key);
        console.log(key + " 入队");

        if (head === null) {
            head = newNode;
            tail = newNode;
        }
        else {
            newNode.next = head;
            head.pre = newNode;
            head = newNode;
        }
    }

    //出队
    this.pop = function () {
        if (tail === null) {
            console.log("队列已空");
        } else {
            console.log(tail.key + " 出队");

            let temp = tail;
            tail = tail.pre;
            tail.next = null;

            temp.next = null;
            return temp;
        }
    }

    //遍历
    this.print = function () {
        let temp = tail;
        let str = "";
        while (tail) {
            str += " " + tail.key;
            tail = tail.pre;
        }
        console.log(str);
        tail = temp;
        temp = null;
    }

    this.getLength = function () {
        let len = 0;
        let temp = tail;
        while (tail) {
            len++;
            tail = tail.pre;
        }
        tail = temp;
        temp = null;
        return len;
    }

}

// node queue/queue.js
let nodes = [89, 34, 98, 12, 88];
let queue = new Queue();
let len = nodes.length;
for (let i = 0; i < len; i++) {
    queue.push(nodes[i]);
}
console.log("-----console------");
queue.print();
console.log("------------------");
queue.pop();
queue.pop();
queue.pop();
console.log("-----console------");
queue.print();
console.log("------------------");
queue.push(1);
queue.push(2);
queue.push(3);
console.log("-----length-------");
console.log("队列的长度：" + queue.getLength());
console.log("-----console------");
queue.print();
console.log("------------------");
queue.pop();
queue.pop();
queue.push(77);
console.log("-----length-------");
console.log("队列的长度：" + queue.getLength());
console.log("-----console------");
queue.print();
